package com.sumfun.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sumfun.blog.common.BaseResponse;
import com.sumfun.blog.common.ErrorCode;
import com.sumfun.blog.common.PageRequest;
import com.sumfun.blog.common.ResultUtils;
import com.sumfun.blog.exception.BusinessException;
import com.sumfun.blog.model.domain.TravelCheckins;
import com.sumfun.blog.model.domain.Websitetool;
import com.sumfun.blog.service.WebsitetoolService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 旅行打卡Controller
 *
 * @author sumfun
 * @date 2024-04-21
 */
@Api(value = "网站便捷管理工具", tags = {"网站便捷管理工具"})
@RestController
@RequestMapping("/websitetool")
public class WebsitetollController {
    
    @Resource
    private WebsitetoolService websitetoolService;

    @GetMapping("/add")
    public BaseResponse<Boolean> addWebsitetool(String url, String name, String classes,HttpServletRequest request){
        //判空
        if(url == null || name == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean save = websitetoolService.addWebsitetool(url, name, classes);
        return ResultUtils.success(save);
    }

    @DeleteMapping("/{id}")
    public BaseResponse<Boolean> deleteWebsitetool(@PathVariable Long id) {
        if(id < 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = websitetoolService.removeById(id);
        return ResultUtils.success(res);
    }

    @GetMapping("/updateInfo")
    public BaseResponse<Boolean> updateWebsitetoolInfo(Long id, String url, String name, String classes,HttpServletRequest request) {
        if(id == null || url == null || name == null || classes == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = false;
        try {
            res = websitetoolService.updateWebsitetool(id ,url, name, classes);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return ResultUtils.success(res);
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> updateWebsitetool(@RequestBody Websitetool websitetool, HttpServletRequest request) {
        if (websitetool == null || websitetool.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 假设 websitetoolService 是一个 MyBatis-Plus 的服务类，它有一个 updateById 方法
        boolean res = websitetoolService.updateById(websitetool); // 这将更新所有非空字段
        return ResultUtils.success(res);
    }

    @GetMapping("/get")
    public BaseResponse<Websitetool> getWebsitetoolById(long id) {
        if(id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Websitetool websitetool = websitetoolService.getById(id);
        return ResultUtils.success(websitetool);
    }

    @GetMapping("/list")
    public BaseResponse<List<Websitetool>> getWebsitetoolList() {
        List<Websitetool> websitetoolList;
        websitetoolList = websitetoolService.list();
        return ResultUtils.success(websitetoolList);
    }

    @GetMapping("/getClassesList")
    public BaseResponse<List> getClassesList() {
        ArrayList arrayList = websitetoolService.getClassesList();
        return ResultUtils.success(arrayList);
    }
    //
    @GetMapping("/list/page")
    public BaseResponse<Page> getWebsitetoolListByPage(PageRequest websitetoolQueryRequest) {
        Page<Websitetool> page = new Page(websitetoolQueryRequest.getPageNum(), websitetoolQueryRequest.getPageSize());
        // 创建一个 QueryWrapper 对象，用于构建查询条件
        QueryWrapper<Websitetool> queryWrapper = new QueryWrapper<>();
        // 添加排序条件，这里按照 visitCount 字段降序排序
        queryWrapper.orderByDesc("visit_count");
        Page<Websitetool> page1 = websitetoolService.page(page, queryWrapper);
        return ResultUtils.success(page1);
    }

}
